package com.cy.demo1.service.impl;

import com.cy.demo1.algorithm.App;
import com.cy.demo1.entity.Data;
import com.cy.demo1.entity.Result1;
import com.cy.demo1.entity.Result2;
import com.cy.demo1.mapper.AlgorithmMapper;
import com.cy.demo1.mapper.DataMapper;
import com.cy.demo1.service.IAlgorithmService;
import org.springframework.stereotype.Service;
import com.cy.demo1.algorithm.miamiayyds.Function.NBfunction;

import javax.annotation.Resource;
import java.io.IOException;

@Service
public class AlgorithmServiceImpl implements IAlgorithmService{

    @Resource
    private AlgorithmMapper algorithmMapper;
    @Resource
    private DataMapper dataMapper;

    @Override
    public Result1 getResult1(Integer projectId) throws IOException {
        Result1 result1;
        result1 = algorithmMapper.find1ByPid(projectId);
        if(result1 == null) {
            Data data;
            data = dataMapper.listProject(projectId);
            NBfunction app = new NBfunction();
            result1 = app.getResult1(data.getProjectData());
            result1.setProjectId(projectId);
            result1.setProjectName(data.getProjectName());
            algorithmMapper.insertResult1(result1);
            return result1;
        }
        else {
            return result1;
        }
    }

    @Override
    public Result2 getResult2(Integer projectId){
        Result2 result2;
        result2 = algorithmMapper.find2ByPid(projectId);
        if(result2 == null) {
            Data data;
            data = dataMapper.listProject(projectId);
            App app = new App();
            result2 = app.getResult2(data.getProjectData());
            result2.setProjectId(projectId);
            result2.setProjectName(data.getProjectName());
            algorithmMapper.insertResult2(result2);
            //System.out.println(result2);
            return result2;
        }
        else {
            //System.out.println(result2);
            return result2;
        }
    }
}
